package com.example.activitysys.controller;

import com.example.activitysys.service.LostItemService;
import com.example.activitysys.service.ClaimRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * 管理员控制器（精简版）
 */
@Controller
public class AdminController {
    
    private final LostItemService lostItemService;
    private final ClaimRecordService claimRecordService;
    
    public AdminController(LostItemService lostItemService,
                          ClaimRecordService claimRecordService) {
        this.lostItemService = lostItemService;
        this.claimRecordService = claimRecordService;
    }
    
    /**
     * 登录页面
     */
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }
    
    /**
     * 登录处理
     */
    @PostMapping("/login")
    public String login(@RequestParam String username, 
                       @RequestParam String password,
                       HttpSession session,
                       Model model) {
        // 简单用户名密码验证（实际项目中应该使用数据库验证）
        if ("admin".equals(username) && "123456".equals(password)) {
            session.setAttribute("admin", "logged_in");
            return "redirect:/dashboard";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }
    
    /**
     * 仪表盘页面
     */
    @GetMapping("/dashboard")
    public String dashboard(HttpSession session, Model model) {
        // 检查登录状态
        if (session.getAttribute("admin") == null) {
            return "redirect:/login";
        }
        
        // 获取统计数据
        long lostItemCount = lostItemService.count();
        long claimRecordCount = claimRecordService.count();
        
        model.addAttribute("lostItemCount", lostItemCount);
        model.addAttribute("claimRecordCount", claimRecordCount);
        
        return "dashboard";
    }
    
    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/login";
    }
}